//
//  ZRRefreshView.swift
//  Sina
//
//  Created by 张锐 on 16/3/4.
//  Copyright © 2016年 zhangr. All rights reserved.
//

import UIKit

enum RefreshStatus: Int{

    case Normal = 0
    
    case Pulling = 1
    
    case Loading = 2

}

class ZRRefreshView: UIControl {
    
    var tableView : UITableView?
    
    var refreshStatus : RefreshStatus = .Normal{
    
        didSet{
        
            switch refreshStatus{
            
            case .Normal:
                
                textLabel.text = "下拉刷新"
            
            arrowImageView.transform = CGAffineTransformIdentity
                
            case .Pulling:
                
                arrowImageView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
                
                textLabel.text = "释放刷新"
                
            case .Loading:
                
                textLabel.text = "加载数据中.."
                
                arrowImageView.hidden = true
                
                waitImageView.hidden = false
                
                
                var inset = tableView?.contentInset
                
                inset?.top += 44
                
                tableView?.contentInset = inset!
                
                sendActionsForControlEvents(UIControlEvents.ValueChanged)
                
            
            }
        
        
        }
    
    
    }
  
    /**
     刷新后执行
     */
    func endRefreshView(){
        
        refreshStatus = .Normal
        
        var inset = tableView?.contentInset
        
        inset?.top -= 44
        
        tableView?.contentInset = inset!
        
        waitImageView.hidden = true
        
        arrowImageView.hidden = false
        
    }

    //重写构造函数
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
//        backgroundColor = UIColor.greenColor()
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件设置约束
    
    private func setupUI(){
        
        addSubview(arrowImageView)
        
        addSubview(textLabel)
        
        addSubview(waitImageView)
        
        self.frame = CGRectMake(0, -44, UIScreen.mainScreen().bounds.width, 44)
        //设置约束
        
        arrowImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(self.snp_centerY)
            
            make.centerX.equalTo(self.snp_centerX).offset(-50)
            
        }
        
        textLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(self.snp_centerY)
            
            make.centerX.equalTo(self.snp_centerX).offset(30)
        }
        
        waitImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(arrowImageView.snp_center)
        }
        
    }
    
    //懒加载控件
    //箭头图标
    lazy var arrowImageView : UIImageView = {
    
        let arrowImageView = UIImageView()
        
        arrowImageView.image = UIImage(named: "tableview_pull_refresh")
        
        return arrowImageView
    
    }()
    //下拉刷新视图文字
    lazy var textLabel : UILabel = {
    
        let textLabel = UILabel()
        
        textLabel.text = "下拉刷新"
        
        textLabel.textColor = UIColor.darkGrayColor()
        
        textLabel.font = UIFont.systemFontOfSize(14)
        
        return textLabel
        
    }()
    //圆圈图标
    lazy var waitImageView : UIImageView = {
    
        let waitImageView = UIImageView()
        
        waitImageView.image = UIImage(named: "tableview_loading")
        
        waitImageView.hidden = true
        
        return waitImageView
    }()
}
// MARK: - 添加监听
extension ZRRefreshView{

    override func willMoveToSuperview(newSuperview: UIView?) {
        
        super.willMoveToSuperview(newSuperview)
        
         tableView = newSuperview as? UITableView
        
        
        //添加监听
        tableView?.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
    }

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        let tableView = object as? UITableView
        
        if tableView!.dragging {
            
            if tableView!.contentOffset.y < -128{
                
                refreshStatus = .Pulling
                
            }else if tableView!.contentOffset.y >= -128 && tableView?.contentOffset.y <= -64{
                
                refreshStatus = .Normal
                
            }
        }else {
            
            if tableView?.contentOffset.y < -128 && refreshStatus == .Pulling{
                
                refreshStatus = .Loading
                
                
            }
            
        }
        
    }
    

}
